<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no,viewport-fit=cover" />
		<title>login</title>
		<!-- css代码 start -->
		<link href="css/global.css" rel="stylesheet" />
		<link href="css/login.css" rel="stylesheet" />
		<link href="css/mui.min.css" rel="stylesheet" />
		<style>
			body{
				background: #fff;
			}
			.mui-content{
				background: #fff;
			}
			.mui-input-row .mui-input-password~.mui-icon-eye, .mui-input-row .mui-input-clear~.mui-icon-clear{
				top: 0;
			}
			.mui-btn:disabled, button:disabled{
				background: #c4c4c4 !important;
				border: 1px solid #c4c4c4 !important;
			}
		
		</style>
		<!-- css代码 end -->
	</head>
	<body>

		<!-- html代码 start -->
		<div class="l_top">
			<img src="images/logo2.png" width="100%"  style="pointer-events:none;"/>
		</div>
		<div class="mui-content">

			<div class="mui-input-row email">
				<input id='account' type="text" class="mui-input-clear mui-input" placeholder="请输入账号" value="">
			</div>
			<div class="mui-input-row pwd">
				<input id='password' type="password" class="mui-input-password mui-input" placeholder="请输入密码" value="">
			</div>
			
		<!-- 	<div class="agreement">
				<input type="checkbox" id="termsOfServiceCheckbox" onchange="termsOfService(this)">接受<span id="termsOfService">《隐私声明》</span>并同意登录
			</div> -->
			
			<div class="shopL">
			      <label><input type="checkbox" id="termsOfServiceCheckbox" onchange="termsOfService(this)"></label>
				  <div class="shopLtext">接受<span id="termsOfService" style="color: #e72226;">《隐私声明》</span>并同意登录</div>
			</div>
			
			<div class="mui-content-padded m">
				<button id='login' class="mui-btn mui-btn-block mui-btn-primary" disabled="disabled">登录</button>
			</div> 

		</div>
		<div class="phone">客服联系电话：400 650 5118  </div>

		<!-- html代码 end -->

		<!-- js代码 start -->

		<!-- 基础js start -->
		<script src="js/mui.min.js"></script>
		<script src="js/utils/h.js"></script>
		<script src="js/constant.js"></script>
		<script src="js/common.js"></script>
		<script src="js/utils/base64.js"></script>
		<script src="js/utils/md5.js"></script>
		<script src="js/utils/sortMap.js"></script>
		<script src="js/utils/http.js"></script>
		<script src="js/app.js"></script>
		<script src="js/sqliteTools.js"></script>
		<!-- 基础js end -->

		<script src="js/mui.enterfocus.js"></script>
		<script src="js/fse/login/login.js"></script>
		<script src="js/utils/jpush.js"></script>
		<script src="js/JPushTools.js"></script>
		<script type="text/javascript" charset="utf-8">
			/**
			 * 初始化数据库
			 */
			function initDB() {
				var sqliteTool = new sqliteTools();
				sqliteTool.openDatabase(function(){
					sqliteTool.initSqlite();
				}); 
			}
			
			/**
			 * 服务条款复选款改变事件
			 */
			function termsOfService(_this) {
				if (_this.checked) {
					h('#login').removeAttr('disabled');
				} else {
					h('#login').attr('disabled', 'disabled');
				}
			}

			/**
			 * 权限提示框
			 */
			function openPermissionLocationAlert() {
				mui.confirm('访问位置权限暂未开启', '提示', ['暂不设置', '设置'], function(e) {
					if (e.index == 1) {
						app.openPermissionSettings();
					}
				})
			}
			
			/**
			 * app更新提示框
			 * 
			 * @param {Object} desc
			 * @param {Object} url
			 */
			function appUpdate(desc, url) {
				plus.nativeUI.alert(desc, function(){
						plus.runtime.openURL(url)
				}, "更新提示", "更新");
			}
				
			/**
			 * 版本检查更新
			 */
			function checkAppVersion() {
				ajaxGetNoneToken(BASE_URL + 'cc/app/latestApp', {}, function(data) {
					return;
					// console.log(JSON.stringify(data))
					if (data.code === 200) {
						var versionCode = data.data.versionCode;
						if (versionCode > plus.runtime.versionCode) {
							appUpdate(data.data.versionDesc, data.data.appUrl)
						}
					}
				}, function(error) {

				});
			}
			
			/**
			 * 运行环境从后台切换到前台事件
			 * 
			 * @param {Object} e
			 */
			function onAppReume(e) {
				console.log("Application foreground!");
				checkAppVersion();
				plus.geolocation.getCurrentPosition(function(p) {}, function(e) {
					if (!app.judgePermissionLocation()) {
						openPermissionLocationAlert()
					}
				});
			}

			(function($, doc) {
				$.init({
					statusBarBackground: '#ea1e1a'
				})
				
				window.addEventListener('closeOthersWebview', function() {
					if (plus.os.name == 'iOS') {
						try{
							window.plus.Push.clearAllLocalNotificationsIniOS();
							window.plus.Push.resetBadge();
							window.plus.Push.setApplicationIconBadgeNumber(0);
						}catch(e){
					
						}
					}
					for (var i = 0; i < 3; i++) {
						h('#login').attr('disabled', 'disabled');
						document.getElementById('termsOfServiceCheckbox').checked = false;
						var webviews = plus.webview.all();
						var launchWebview = plus.webview.getLaunchWebview();
						for (var i = 0; i < webviews.length; i++) {
							console.log(webviews[i].id);
							if (launchWebview != webviews[i]) {
								webviews[i].close();
							}
						}
						setTimeout(function(){
							plus.nativeUI.closeToast();
							plus.nativeUI.closeWaiting();
						}, 2000)
					}
				}, false);

				$.plusReady(function() {
					plus.screen.lockOrientation("portrait-primary");
					var settings = app.getSettings()
					var state = app.getState();
					
					if(h.getItem('account')!=null && h.getItem('account')!=undefined && h.getItem('account')!='undefined' && h.getItem('account')!=''){
						h('#account').val(h.getItem('account'))
						h('#password').val(h.getItem('password'))
					}
					
					// 初始化一下定位，初始化失败提示权限问题
					plus.geolocation.getCurrentPosition(function(p) {}, function(e) {
						if (!app.judgePermissionLocation()) {
							openPermissionLocationAlert()
						}
					});
					
					// 当前版本缓存
					var versionCode = h.getItem("versionCode");
					if (!versionCode) {
						h.setItem("versionCode", plus.runtime.versionCode + '')
					}

					// 运行环境从后台切换到前台事件
					document.addEventListener("resume", onAppReume, false);

					// var mainPage = plus.webview.getWebviewById("main");
					// var main_loaded_flag = false;
					// if (!mainPage) {
					// 	mainPage = $.preload({
					// 		"id": 'main',
					// 		"url": 'main.html'
					// 	});
					// } else {
					// 	main_loaded_flag = true;
					// }

					// mainPage.addEventListener("loaded", function() {
					// 	main_loaded_flag = true;
					// });

					var toMain = function() {
						//使用定时器的原因：
						//可能执行太快，main页面loaded事件尚未触发就执行自定义事件，此时必然会失败
						// var id = setInterval(function() {
						// 	if (main_loaded_flag) {
						// 		clearInterval(id);
						// 		$.fire(mainPage, 'show', null);
						// 		mainPage.show("pop-in");
						// 	}
						// }, 20);

						app.jumpTo("main.html", "main");
					};

					

					//检查 "登录状态" 开始
					if (state.user && state.token) {
						// plus.navigator.setStatusBarBackground("#FFFFFF")
						var versionCode = h.getItem("versionCode");
						if (versionCode && versionCode == plus.runtime.versionCode) {
							toMain();
						}
					}
					
					// 初始化本地数据库
					initDB();
					
					// 检查更新
					checkAppVersion();
					
					// 初始化JPush
					initPush();

					// close splash
					setTimeout(function() {
						//关闭 splash
						plus.navigator.closeSplashscreen();
					}, 2000);

					//检查 "登录状态" 结束

					var loginButton = h('#login')[0];
					h('#login').tap(function() {
						app.hideSoftKeybord()
						if (!app.judgePermissionLocation()) {
							openPermissionLocationAlert()
							return;
						}
						checkAppVersion();
						var loginInfo = {
							account: h('#account').val(),
							password: h('#password').val()
						};
						app.showWaiting("登录中...")
						login.login(loginInfo, function(err) {
							app.closeWaiting()
							if (err) {
								plus.nativeUI.toast(err);
								return;
							} 
							h.setItem('account', loginInfo.account);
							h.setItem('password', loginInfo.password);
							getRegistrationID()
							toMain();

						});
					});
					$.enterfocus('#login-form input', function() {
						$.trigger(loginButton, 'tap');
					});

					h('#termsOfService').tap(function() {
						app.jumpTo('view/termsOfService/termsOfService.html', 'view/termsOfService/termsOfService.html');
					})

					var backButtonPress = 0;
					$.back = function(event) {
						backButtonPress++;
						if (backButtonPress > 1) {
							plus.runtime.quit();
						} else {
							plus.nativeUI.toast('再按一次退出应用');
						}
						setTimeout(function() {
							backButtonPress = 0;
						}, 1000);
						return false;
					};
				});
			}(mui, document))
		</script>
		<!-- js代码 end -->
	</body>
</html>
